package com.ljx.Filter;


import com.alibaba.fastjson.JSON;
import com.ljx.Domain.MyThreadLocal;
import com.ljx.Domain.R;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter(filterName = "logincheckFiler",urlPatterns = "/*")
public class LogincheckFiler implements Filter {
        public static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        //定义不需要处理的请求
        String[] url = new String[]{
                "/employee/login",
                "/employee/logout",
                "/backend/**",
                "/front/**",
                "/common/upload",
                "/common/download",
                "/user/sendMsg",
                "/user/login"
        };

        String requestURI = httpServletRequest.getRequestURI();
        log.info("拦截到请求{}",requestURI);
        //判断路径是否需要处理
        Boolean check = check(url, requestURI);
        if(check){
            log.info("本次请求{}不需要处理",requestURI);
            chain.doFilter(httpServletRequest,httpServletResponse);
            return ;
        }
        //判断是否登录
        if(httpServletRequest.getSession().getAttribute("employee")!=null){
            log.info("用户已登录id为：{}",httpServletRequest.getSession().getAttribute("employee"));
            MyThreadLocal.setThreadLocal((Long) httpServletRequest.getSession().getAttribute("employee"));
            chain.doFilter(httpServletRequest,httpServletResponse);
//            清除threadlocal
        MyThreadLocal.clearThreadLocal();
            return ;
        }
        //判断手机端是否登录
        if(httpServletRequest.getSession().getAttribute("user")!=null){
            log.info("用户已登录id为：{}",httpServletRequest.getSession().getAttribute("user"));
            MyThreadLocal.setThreadLocal((Long) httpServletRequest.getSession().getAttribute("user"));
            chain.doFilter(httpServletRequest,httpServletResponse);
//            清除threadlocal
            MyThreadLocal.clearThreadLocal();
            return ;
        }
        //未登录返回主页面，前端写了跳转，直接返回json数据
        log.info("用户未登录");
        httpServletResponse.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
    }

    /**
     * 检查路径uri
     * @param urls
     * @param requestURI
     * @return
     */
    public Boolean check(String[] urls,String requestURI){
        boolean match= false;
        for (String url : urls) {
            //判断requestURI请求是否在urls数组中，在返回true,不在返回false
            match = ANT_PATH_MATCHER.match(url, requestURI);
            if(match){
                break;
            }
        }
        return match;
    }
}
